package com.nit.lab.service.medical.impl;

import com.nit.lab.dto.medical.InfoDTO;
import com.nit.lab.dto.medical.SessionDTO;
import com.nit.lab.dto.utils.PagingDTO;
import com.nit.lab.dto.utils.TableReqDTO;
import com.nit.lab.dto.utils.TableRspDTO;
import com.nit.lab.entity.medical.SessionEntity;
import com.nit.lab.mapper.medical.SessionMapper;
import com.nit.lab.service.medical.SessionService;
import ma.glasnost.orika.MapperFactory;
import ma.glasnost.orika.impl.DefaultMapperFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class SessionServiceImpl implements SessionService {

    private final SessionMapper sessionMapper;

    @Autowired(required = false)
    public SessionServiceImpl(SessionMapper SessionMapper) {
        this.sessionMapper = SessionMapper;
    }


    @Override
    public TableRspDTO list4Table(TableReqDTO tableReqDTO) throws Exception {
        Long count = sessionMapper.count4Table(
                tableReqDTO.parseQueryParam("ecgUsername"),tableReqDTO.parseQueryParam("idDevice"));
        List<SessionDTO> sessionDTOS = sessionMapper.list4Table(
                tableReqDTO.getStart(),
                tableReqDTO.getPageSize(),
                tableReqDTO.parseQueryParam("ecgUsername"),
                tableReqDTO.parseQueryParam("idDevice"),
                tableReqDTO.getSortField(),
                tableReqDTO.getSortOrder());
        PagingDTO pagingDTO = new PagingDTO(
                tableReqDTO.getPageNo(),
                tableReqDTO.getPageSize(),
                count,
                sessionDTOS);
        for (int i = 0; i < sessionDTOS.size(); i++) {
            sessionDTOS.get(i).setOrderNumber(i+1+tableReqDTO.getStart());
        }
        TableRspDTO tableRspDTO = new TableRspDTO(pagingDTO);
        return tableRspDTO;
    }

    @Override
    public Integer add(SessionDTO SessionDTO) throws Exception {
        MapperFactory mapperFactory = new DefaultMapperFactory.Builder().build();
        SessionEntity SessionEntity = mapperFactory.getMapperFacade().map(SessionDTO, SessionEntity.class);
        return sessionMapper.add(SessionEntity);
    }

    @Override
    public Integer update(SessionDTO SessionDTO) throws Exception {
        MapperFactory mapperFactory = new DefaultMapperFactory.Builder().build();
        SessionEntity SessionEntity = mapperFactory.getMapperFacade().map(SessionDTO, SessionEntity.class);
        return sessionMapper.update(SessionEntity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer remove(String SessionIds) throws Exception {
        String[] arraySessionId = SessionIds.split(",");
        for (int i = 0; i < arraySessionId.length; i++) {
            System.out.println(arraySessionId[i]);
            sessionMapper.remove(Integer.parseInt(arraySessionId[i]));
        }
        return arraySessionId.length;
    }

    @Override
    public List<InfoDTO> getInfo(InfoDTO infoDTO) throws Exception {
        List<InfoDTO> res = sessionMapper.getInfo(infoDTO.getId());
        return res;
    }
}
